//
//  RegisterViewController.m
//  huashiedu
//
//  Created by oceans on 2018/7/29.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "RegisterViewController.h"
#import "Utility.h"
#import "TipManager.h"

@interface RegisterViewController ()

@property (strong, nonatomic) UITextField *username;
@property (strong, nonatomic) UITextField *code;
@property (strong, nonatomic) UITextField *password;
@property (strong, nonatomic) UITextField *confirmPassword;
@property (strong, nonatomic) UIButton *getCodeButton;
@property (strong, nonatomic) UIButton *putButton;
@property (strong, nonatomic) NSTimer *timer;
@property (assign, nonatomic) double timertime;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.timer invalidate];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    if (_isRegister) {
        self.navigationItem.title = @"注册";
    }else {
        self.navigationItem.title = @"找回密码";
    }
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.view addGestureRecognizer:tap];
    
    self.username = [[UITextField alloc] initWithFrame:CGRectMake(31, XNavigationHeight + 30, ScreenWidth - 62, 35)];
    _username.placeholder = @"请输入手机号";
    _username.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:_username];
    
    self.getCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _getCodeButton.frame = CGRectMake(0, 0, 113, 33);
    [_getCodeButton setTitleColor:COLOR(151, 151, 151, 1) forState:UIControlStateNormal];
    _getCodeButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [_getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_getCodeButton addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
    _username.rightView = _getCodeButton;
    _username.rightViewMode = UITextFieldViewModeAlways;
    
    CALayer *line1 = [[CALayer alloc] init];
    line1.frame = CGRectMake(30, CGRectGetMaxY(_username.frame) + 6.5, ScreenWidth - 60, 0.5);
    line1.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:line1];
    
    self.code = [[UITextField alloc] initWithFrame:CGRectMake(31, CGRectGetMaxY(line1.frame) + 17, ScreenWidth - 62, 35)];
    _code.placeholder = @"请输入验证码";
    [self.view addSubview:_code];
    
    CALayer *line2 = [[CALayer alloc] init];
    line2.frame = CGRectMake(30, CGRectGetMaxY(_code.frame) + 6.5, ScreenWidth - 60, 0.5);
    line2.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:line2];
    
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(31, CGRectGetMaxY(line2.frame) + 17, ScreenWidth - 62, 35)];
    if (_isRegister) {
        _password.placeholder = @"请输入密码（6-20位，数字+字母）";
    }else {
        _password.placeholder = @"请输入新密码（6-20位，数字+字母）";
    }
    _password.secureTextEntry = YES;
    [self.view addSubview:_password];
    
    CALayer *line3 = [[CALayer alloc] init];
    line3.frame = CGRectMake(30, CGRectGetMaxY(_password.frame) + 6.5, ScreenWidth - 60, 0.5);
    line3.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:line3];
    
    self.confirmPassword = [[UITextField alloc] initWithFrame:CGRectMake(31, CGRectGetMaxY(line3.frame) + 17, ScreenWidth - 62, 35)];
    _confirmPassword.placeholder = @"确认密码";
    _confirmPassword.secureTextEntry = YES;
    [self.view addSubview:_confirmPassword];
    
    CALayer *line4 = [[CALayer alloc] init];
    line4.frame = CGRectMake(30, CGRectGetMaxY(_confirmPassword.frame) + 6.5, ScreenWidth - 60, 0.5);
    line4.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:line4];
    
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    login.frame = CGRectMake(31, CGRectGetMaxY(line4.frame) + 35.0, ScreenWidth - 62, 50);
    login.backgroundColor = COLOR(253, 119, 77, 1);
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    login.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [login setTitle:@"提交" forState:UIControlStateNormal];
    [login addTarget:self action:@selector(toPut) forControlEvents:UIControlEventTouchUpInside];
    login.layer.cornerRadius = 3.0;
    login.layer.masksToBounds = YES;
    [self.view addSubview:login];
}

- (void)getCode {
    
    __weak typeof(self) weakself = self;
    if ([Utility checkMobile:_username.text]) {
        [self.view endEditing:YES];
        if (_isRegister) {
            _timertime = 60;
            [self startTimer];
            [[UserModel shared] getCodeWithPhoneNumber:_username.text scope:@"register" successBlock:^{
                
            } failedBlock:^{
                [weakself stopTimer];
            }];
        }else {
            _timertime = 60;
            [self startTimer];
            [[UserModel shared] getCodeWithPhoneNumber:_username.text scope:@"forget" successBlock:^{
                
            } failedBlock:^{
                [weakself stopTimer];
            }];
        }
    }
}

- (void)startTimer {
    
    _getCodeButton.enabled = NO;
    [_getCodeButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_getCodeButton setTitle:[NSString stringWithFormat:@"(%.0f)秒后可重发", _timertime] forState:UIControlStateNormal];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
}

- (void)timerMethod {
    
    if (_timertime <= 0) {
        [_getCodeButton setTitleColor:COLOR(235, 86, 0, 1) forState:UIControlStateNormal];
        [_getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        _getCodeButton.enabled = YES;
        [self.timer invalidate];
        return;
    }
    _timertime = _timertime - 1;
    [_getCodeButton setTitle:[NSString stringWithFormat:@"(%.0f)秒后可重发", _timertime] forState:UIControlStateNormal];
}

- (void)stopTimer {
    [_getCodeButton setTitleColor:COLOR(235, 86, 0, 1) forState:UIControlStateNormal];
    [_getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    _getCodeButton.enabled = YES;
    [self.timer invalidate];
}

- (void)toPut {
    
    if (![Utility checkMobile:_username.text]) {
        return;
    }
    if (_code.text.length == 0) {
        return;
    }
    if (![Utility checkPassword:_password.text]) {
        [TipManager showTip:@"密码不合法"];
        return;
    }
    if (![_password.text isEqualToString:_confirmPassword.text]) {
        [TipManager showTip:@"密码不合法"];
        return;
    }
    [self.view endEditing:YES];
    __weak typeof(self) weakself = self;
    if (_isRegister) {
        [[UserModel shared] registerWithName:_username.text code:_code.text password:_password.text successBlock:^{
            [weakself.navigationController dismissViewControllerAnimated:YES completion:nil];
        }];
    }else {
        [[UserModel shared] findPassword:_username.text code:_code.text password:_password.text successBlock:^{
            [TipManager showTip:@"修改成功"];
            [weakself.navigationController popViewControllerAnimated:YES];
        }];
    }
}

- (void)hideKeyboard {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
